home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 March / macformat-048.iso / Useful Mac Tools / strip / Source Kit ƒ / StripPPC / StripFile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  2.8 KB  |  122 lines  |  [TEXT/MMCC]

  1. /*    stripfile.cpp
  2.  *
  3.  *        This contains the actual strip code. There are two versions of this
  4.  *    file, one for the 68K version, and one for the PPC version
  5.  */
  6.  
  7. #include "Strip.h"
  8. #include "cfrg.h"
  9.  
  10. /************************************************************************/
  11. /*                                                                        */
  12. /*    Error Display Code                                                    */
  13. /*                                                                        */
  14. /************************************************************************/
  15.  
  16. /*    DisplayAlert
  17.  *
  18.  *        Display the alert, given the application name and message
  19.  */
  20.  
  21. static void DisplayAlert(FSSpec *file, short id, short code)
  22. {
  23.     unsigned char buffer[256];
  24.     unsigned char codeStr[32];
  25.     
  26.     GetIndString(buffer,128,id);
  27.     if (code) {
  28.         NumToString(code,codeStr);
  29.         ParamText(file->name,buffer,codeStr,NULL);
  30.     } else {
  31.         ParamText(file->name,buffer,NULL,NULL);
  32.     }
  33.     Alert(130,NULL);
  34. }
  35.  
  36. /************************************************************************/
  37. /*                                                                        */
  38. /*    Main entry point                                                    */
  39. /*                                                                        */
  40. /************************************************************************/
  41.  
  42. /*    StripFile
  43.  *
  44.  *        Determine if this file is the correct one by type and temperment;
  45.  *    if it is, then go ahead and strip the code resources and replace them
  46.  *    with one that presents a very simple message.
  47.  */
  48.  
  49. void StripFile(FSSpec *file)
  50. {
  51.     FInfo finfo;
  52.     short appRefNum;
  53.     short err;
  54.     CFRGResource **cfrg;
  55.     
  56.     /*
  57.      *  Figure out what the file type is.
  58.      */
  59.     
  60.     if (FHasFSSpec) FSpGetFInfo(file,&finfo);
  61.     else HGetFInfo(file->vRefNum,file->parID,file->name,&finfo);
  62.     
  63.     if (finfo.fdType != 'APPL') {
  64.         DisplayAlert(file,1,0);
  65.         return;
  66.     }
  67.     
  68.     /*
  69.      *    Now open this application for modification.
  70.      */
  71.     
  72.     if (FHasFSSpec) appRefNum = FSpOpenResFile(file,fsRdWrPerm);
  73.     else appRefNum = HOpenResFile(file->vRefNum,file->parID,file->name,fsRdWrPerm);
  74.     if (appRefNum == -1) {
  75.         DisplayAlert(file,2,0);
  76.         return;
  77.     }
  78.         
  79.     /*
  80.      *  Is there a code fragment resource?
  81.      */
  82.      
  83.     cfrg = (CFRGResource **)Get1Resource('cfrg',0);
  84.     if (cfrg == NULL) {
  85.         CloseResFile(appRefNum);
  86.         DisplayAlert(file,3,0);
  87.         return;
  88.     }
  89.     if (((**cfrg).version != 1) || ((**cfrg).numFrags != 1) ||
  90.         ((**cfrg).frags[0].type != 'pwpc') || ((**cfrg).frags[0].usage != 1) ||
  91.         ((**cfrg).frags[0].loc != 1) || ((**cfrg).frags[0].offset != 0) ||
  92.         ((**cfrg).frags[0].length != 0)) {
  93.         
  94.         ReleaseResource((Handle)cfrg);
  95.         CloseResFile(appRefNum);
  96.         DisplayAlert(file,4,0);
  97.         return;
  98.     }
  99.      
  100.     /*
  101.      *  Remove the fragment resource.
  102.      */
  103.      
  104.     SetResAttrs((Handle)cfrg,0);
  105.     RemoveResource((Handle)cfrg);
  106.     if (0 != (err = ResError())) {
  107.         ReleaseResource((Handle)cfrg);
  108.         CloseResFile(appRefNum);
  109.         DisplayAlert(file,5,err);
  110.         return;
  111.     }
  112.     
  113.     UpdateResFile(appRefNum);
  114.     CloseResFile(appRefNum);
  115.     
  116.     if (FHasFSSpec) FSpOpenDF(file,fsRdWrPerm,&appRefNum);
  117.     else HOpen(file->vRefNum,file->parID,file->name,fsRdWrPerm,&appRefNum);
  118.     SetEOF(appRefNum,0L);                    // Clear the current EOF
  119.     FSClose(appRefNum);
  120. }
  121.  
  122.